package com.jreque.queue;

import java.util.*;
import com.jreque.queue.*;
import java.lang.*;

/**
 * Holds register of registered functions by name.
 */
public class NamedFunctionRegister {
    /**
     * The singleton instance
     */
    private static NamedFunctionRegister instance;
    /**
     * Name to function register.
     */
    private HashMap<String, ObjectMethodDescriptor> functions;

    /**
     */
    private NamedFunctionRegister() {
        functions = new HashMap<String, ObjectMethodDescriptor>();
    }

    /**
     * Gets singleton instance of register.
     * @return 
     */
    public static NamedFunctionRegister getInstance() {
        if(instance == null) {
            synchronized(NamedFunctionRegister.class) {
                if(instance == null) {
                    instance = new NamedFunctionRegister();
                }
            }
        }
        return instance;
    }

    /**
     * Registers new function by name with clas and method names to call.
     * @param registeredName A function name to save as.
     * @param className Class name to instantiate during execution.
     * @param methodName A method name to call during execution.
     * @return True if registered successfuly, false if function is already registered.
     */
    public synchronized boolean register(String registeredName, String className, String methodName) {
        if(functions.containsKey(registeredName)) {
            return false;
        }
        functions.put(registeredName, new ObjectMethodDescriptor(className, methodName));
        return true;
    }
    
    /**
     * Retrieves the descriptor for the specified name
     */
    synchronized ObjectMethodDescriptor lookUp(String registeredName) {
        return functions.get(registeredName);
    }
}

